<!--  -->
<template>
  <div class='authority'>
    <el-button v-permissions="'sty:add'" type="primary" @click="addNewSty" style="margin-bottom: 10px;">新增</el-button>
    <el-form :inline="true" :model="formInstyQuery" class="demo-form-inline">
      <el-form-item label="栏舍名称">
        <el-input v-model="formInstyQuery.kw" placeholder="请输入牧场名称"></el-input>
      </el-form-item>
      <el-form-item label="牧场名称">
        <el-select v-model="formInstyQuery.ranchId" placeholder="请选择" @change="changeQuery">
          <el-option
          v-for="(item,index) in formInstyQuery.ranchIdOptions"
          :key="index"
          :label="item.name"
          :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="圈棚名称">
        <el-select :disabled="formInstyQuery.isHaveRanchId" v-model="formInstyQuery.penId" placeholder="请选择">
          <el-option
          v-for="(item,index) in formInstyQuery.penIdOptions"
          :key="index"
          :label="item.penName"
          :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitSrearch">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table
        :data="getMyStyList"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="55">
        </el-table-column>
        <el-table-column
          prop="styName"
          label="栏舍名称"
          width="130">
        </el-table-column>
        <el-table-column
          prop="penName"
          label="卷棚名称"
          width="130">
        </el-table-column>
        <el-table-column
          prop="ranchName"
          label="牧场名称"
          width="130">
        </el-table-column>
        <el-table-column
          prop="ranchRemark"
          label="备注"
          width="130">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          >
          <template slot-scope="scope">
            <el-button v-permissions="'sty:detail'" @click="getStyDetail(scope.row.id)" type="text" size="small">详情</el-button>
            <el-button v-permissions="'sty:delete'" @click="deletRoleUser(scope.row.id)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <!-- 栏舍详情 -->
      <el-dialog
        title="栏舍详情"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :show-close="false"
        :close-on-press-escape="true"
        width="50%">
        <el-form
          ref="styDetailFrom"
          :model="gupdatedStyDetail"
          :disabled="isDisabled"
          :inline="false"
          class="demo-form-inline"
          label-position="left"
          label-width="auto"
          :rules="styDetailsRules">
          <div class="filter-container">
            <el-form-item label="栏舍名称" prop="styName">
              <el-input v-model="gupdatedStyDetail.styName"></el-input>
            </el-form-item>
            <el-form-item label="牧场名称" prop="ranchId">
              <el-select v-model="gupdatedStyDetail.ranchId" placeholder="请选择" @change="changeForDetail">
                <el-option
                v-for="(item,index) in gupdatedStyDetail.ranchIdOptions"
                :key="index"
                :label="item.name"
                :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="圈棚名称" prop="penId">
              <el-select
                v-model="gupdatedStyDetail.penId"
                placeholder="请选择"
                >
                <el-option
                v-for="(item,index) in gupdatedStyDetail.penIdOptions"
                :key="index"
                :label="item.penName"
                :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="gupdatedStyDetail.ranchRemark"></el-input>
            </el-form-item>
          </div>
        </el-form>
        <div class="buttons">
           <el-button type="primary" v-permissions="'sty:update'" @click="setConfigUserRole" v-if="isDisabled">修改</el-button>
           <el-button type="primary" @click="submitConfigUser" v-if="!isDisabled">保存</el-button>
           <el-button @click="closeDiaDetail">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 新增栏舍 -->
      <el-dialog
        title="新增栏舍"
        :visible.sync="addCompany"
        :close-on-click-modal="false"
        :show-close="false"
        :close-on-press-escape="true"
        width="50%">
        <el-form ref="formName" :model="formAddSty" :rules="addUserRule" label-width="80px">
          <el-form-item label="栏舍名称" prop="name">
            <el-input v-model="formAddSty.name"></el-input>
          </el-form-item>
          <el-form-item label="牧场" prop="ranchId">
            <el-select v-model="formAddSty.ranchId" placeholder="请选择" @change="changeRanch">
              <el-option
                v-for="(item,index) in formAddSty.ranchIdOptions"
                :key="index"
                :label="item.name"
                :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="圈棚" prop="penId">
            <el-select @change="changeTest" :disabled="formAddSty.isHaveRanchId" v-model="formAddSty.penId" placeholder="请选择">
              <el-option
              v-for="(item,index) in formAddSty.penIdOptions"
              :key="index"
              :label="item.penName"
              :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formAddSty.remark"></el-input>
          </el-form-item>
        </el-form>
        <div style="width: 100%; text-align: center;">
          <el-button type="primary" @click="addMyNewCompany">保存</el-button>
          <el-button @click="closeDia">关闭</el-button>
        </div>
      </el-dialog>
    </div>
    <pagination
      class="pagination_container"
      :total="pagination.total"
      :page-sizes="[10, 20, 50, 100]"
      :page.sync="pagination.page"
      :limit.sync="pagination.pageSize"
      @pagination="getStyListInit"
    />
  </div>
</template>

<script>
import { styDetail, styAdd, styList, styUpdate, penPenList } from '@/api/sty'
import { ranchRanchList } from '@/api/ranch'
import Pagination from '@/components/pagination'
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    Pagination
  },
  data () {
    // 这里存放数据
    return {
      getMyStyList: [],
      statusArr: [
        {
          status: 1,
          name: '正常'
        },
        {
          status: 0,
          name: '封禁'
        }
      ],
      isDisabled: true,
      gupdatedStyDetail: { // 修改栏舍
        styName: '',
        ranchId: '',
        ranchIdOptions: [],
        isHaveRanchId: true,
        penId: '',
        penIdOptions: [],
        ranchRemark: '',
        styId: ''
      },
      styDetailsRules: {
        styName: [
          { required: true, trigger: 'blur', message: '请输入您的名字' }
        ],
        ranchId: [
          { required: true, trigger: 'change', message: '请选择' }
        ],
        penId: [
          { required: true, trigger: 'change', message: '请选择' }
        ]
      },
      getMyRanch: [], // 牧场列表
      formAddSty: { // 新增
        isHaveRanchId: true,
        name: '',
        ranchId: '',
        ranchIdOptions: [],
        penId: '',
        penIdOptions: [],
        remark: ''
      },
      // 新增 rule 验证
      addUserRule: {
        name: [
          { required: true, trigger: 'blur', message: '请输入您的名字' }
        ],
        ranchId: [
          { required: true, trigger: 'change', message: '请选择' }
        ],
        penId: [
          { required: true, trigger: 'change', message: '请选择' }
        ]
      },
      // 分页
      pagination: {
        page: 1,
        pageSize: 10,
        total: 0
      },
      // 查询
      formInstyQuery: {
        kw: '',
        ranchId: '',
        ranchIdOptions: [],
        penId: '',
        penIdOptions: [],
        isHaveRanchId: true
      },
      // checked: false,
      dialogVisible: false,
      userNameGet: '',
      addCompany: false
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {

  },
  // 方法集合
  methods: {
    changeTest (val) {
      console.log(val)
    },
    // 获取牧场列表
    getRanchListInSty () {
      ranchRanchList().then(res => {
        this.formAddSty.ranchIdOptions = res.data
        this.formInstyQuery.ranchIdOptions = res.data
      })
    },
    // 切换牧场列表 同时展示圈棚 查询
    changeQuery (val) {
      this.formInstyQuery.penId = ''
      penPenList(val).then(res => {
        if (res.code === '200') {
          this.formInstyQuery.isHaveRanchId = false
          this.formInstyQuery.penIdOptions = res.data
        }
      })
    },
    // 切换牧场列表 同时展示圈棚 详情
    changeForDetail (val) {
      console.log(val)
      this.gupdatedStyDetail.penId = ''
      penPenList(val).then(res => {
        if (res.code === '200') {
          this.gupdatedStyDetail.penIdOptions = res.data
          this.$forceUpdate()
        }
      })
    },
    // 切换牧场列表 同时展示圈棚 新增
    changeRanch (val) {
      this.formAddSty.penId = ''
      penPenList(val).then(res => {
        if (res.code === '200') {
          this.formAddSty.isHaveRanchId = false
          this.formAddSty.penIdOptions = res.data
          this.$forceUpdate()
        }
      })
    },
    // 获取栏舍列表
    getStyListInit () {
      styList({
        ranchName: this.formInstyQuery.kw,
        penId: this.formInstyQuery.penId,
        ranchId: this.formInstyQuery.ranchId,
        limit: this.pagination.pageSize,
        page: this.pagination.page
      }).then(res => {
        if (res.code === '200') {
          this.getMyStyList = res.data.records
          this.$forceUpdate()
          if (res.data.total > 0 && Math.ceil(res.data.total / this.pagination.size) < this.pagination.page) {
            this.pagination.page = Math.ceil(res.data.total / this.pagination.size)
          } else {
            this.pagination.total = res.data.total
          }
        } else {
          this.$message({
            message: res.message,
            type: 'warning'
          })
        }
      })
    },
    // 打开新增弹框
    addNewSty () {
      this.getRanchListInSty()
      this.addCompany = true
    },
    // 新增保存
    addMyNewCompany () {
      this.$refs.formName.validate((valid) => {
        if (valid) {
          this.formAddSty.expireTime = new Date(this.formAddSty.expireTime).getTime()
          const StyObj = {
            name: this.formAddSty.name,
            penId: this.formAddSty.penId,
            ranchId: this.formAddSty.ranchId,
            remark: this.formAddSty.remark
          }
          console.log(StyObj)
          styAdd(StyObj).then(res => {
            this.addCompany = false
            if (res.code === '200') {
              this.getStyListInit()
              this.$message({
                message: '操作成功',
                type: 'success'
              })
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
        } else {
          return false
        }
      })
    },
    // 查询
    submitSrearch () {
      this.getStyListInit()
    },
    // 重置
    resetForm () {
      this.formInstyQuery.kw = ''
      this.formInstyQuery.ranchId = ''
      this.formInstyQuery.penId = ''
    },
    // 删除用户
    deletRoleUser (val) {
      // this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning',
      //     center: true
      //   }).then(() => {
      //     companyDelete(val).then(res => {
      //       if (res.code === '200') {
      //         this.$message({
      //           message: '删除成功',
      //           type: 'success'
      //         })
      //         this.getStyListInit()
      //       } else {
      //         this.$message({
      //           type: 'info',
      //           message: res,message
      //         });
      //       }
      //     })
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消删除'
      //     });
      //   });
    },
    // 用户详情
    getStyDetail (val) {
      this.userRoleIdGet = val
      styDetail(val).then(res => {
        this.gupdatedStyDetail = res.data
        this.gupdatedStyDetail.ranchIdOptions = this.formInstyQuery.ranchIdOptions
        this.gupdatedStyDetail.penIdOptions = [
          {
            penName: res.data.penName,
            id: res.data.penId
          }
        ]
        this.dialogVisible = true
      })
    },
    // 修改用户详情
    setConfigUserRole () {
      this.$confirm('是否确认修改栏舍信息', '提示', {
        confirmButtonText: '确定',
        showCancelButton: '取消',
        distinguishCancelAndClose: false
      }).then(() => {
        this.isDisabled = false
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消!'
        })
      })
    },
    // 保存修改后用户信息
    submitConfigUser () {
      this.$refs.styDetailFrom.validate((valid) => {
        if (valid) {
          const role = {
            name: this.gupdatedStyDetail.styName,
            penId: this.gupdatedStyDetail.penId,
            ranchId: this.gupdatedStyDetail.ranchId,
            remark: this.gupdatedStyDetail.ranchRemark,
            styId: this.gupdatedStyDetail.id
          }
          styUpdate(role).then(res => {
            if (res.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.dialogVisible = false
              this.isDisabled = true
              this.getStyListInit()
            } else {
              this.$message({
                type: 'info',
                message: res.message
              })
            }
          })
        }
      })
    },
    // 关闭新增弹框
    closeDia () {
      this.addCompany = false
      this.formAddSty = {
        isHaveRanchId: true,
        name: '',
        ranchId: '',
        ranchIdOptions: [],
        penId: '',
        penIdOptions: [],
        remark: ''
      }
    },
    // 关闭详情弹框
    closeDiaDetail () {
      this.dialogVisible = false
      this.isDisabled = true
    }
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.getStyListInit() // 获取栏舍列表
    this.getRanchListInSty() // 获取牧场options
  }
}
</script>
<style lang='scss' scoped>
  //@import url(); 引入公共css类
.authority {
  width: 100%;
  height: 100%;
}
.fir {
  width: auto;
  height: 30px;
  line-height: 30px;
  span {
    margin-left: 10px;
    padding: 5px;
    width: 30%;
  }
}
.sen {
  margin: 5px auto;
  width: 80%;
  padding: 10px;
  border: 1px solid #f0f0f0;
}
.buttons {
  width: 100%;
  text-align: center;
}
</style>
